{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymongo\n",
    "from pymongo.command_cursor import CommandCursor\n",
    "client = pymongo.MongoClient(\"211.87.227.243\",27017)\n",
    "db = client.get_database(\"bookcrawl\")\n",
    "books = db.get_collection(\"books\")\n",
    "books.create_index(\"press\")\n",
    "# presses = db.get_collection(\"presses\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "cur = books.find()\n",
    "presses_set = set()\n",
    "for res in cur:\n",
    "    if \"press\" in res:\n",
    "        presses_set.add(res[\"press\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "re_press = re.compile(\"(^[^出版社]*出版社)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "re_pre_dict = {}\n",
    "for i in presses_set:\n",
    "    press_match = re.search(re_press,i)\n",
    "    if press_match is not None:\n",
    "        re_pre_dict[i] = press_match.group(1)\n",
    "    else:\n",
    "        re_pre_dict[i] = i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# for i,(pre,new) in enumerate(re_pre_dict.items()):\n",
    "#     if i%10 == 0:\n",
    "#         print(i)\n",
    "#     books.update_many({\"press\":pre},{\"$set\":{\"re_press\":new}})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'_id': ObjectId('5cb5ba0a72bcf98f1d3bd376'),\n",
       " 'field': 'bookschina',\n",
       " 'isbn': '9787539932477',\n",
       " 'title': '群氓时代',\n",
       " 'author': '赫连勃勃大王著',\n",
       " 'press': '江苏文艺出版社',\n",
       " 'star': '94.0',\n",
       " 'price': '12.0',\n",
       " 'type': '文学-中国现当代随笔',\n",
       " 'link': 'http://www.bookschina.com/3903831.htm',\n",
       " 'pid': '3903831',\n",
       " 're_press': '江苏文艺出版社'}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "books.find_one()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# for i,(pre,new) in enumerate(re_pre_dict.items()):\n",
    "#     presses.insert_one({\"press\":pre,\"re_press\":new})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [],
   "source": [
    "# presses.find_one({\"type\":\"all\"})[\"press_list\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "# re_press_list = list(set(re_pre_dict.values()))\n",
    "# presses.insert_one({\"type\":\"all\",\"press_list\":re_press_list})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [],
   "source": [
    "# press_count_dict = {}\n",
    "# cur = books.find()\n",
    "# for res in cur:\n",
    "#     if \"re_press\" not in res:\n",
    "#         print(res)\n",
    "#         continue\n",
    "#     i = press_count_dict.setdefault(res[\"re_press\"],0)\n",
    "#     press_count_dict[res[\"re_press\"]] = i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "presses.create_index(\"re_press\")\n",
    "for key,value in press_count_dict.items():\n",
    "    presses.update_one({\"re_press\":key},{\"$set\":{\"count\":value}})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "cur = presses.find({\"count\":{\"$exists\":True,\"$orderby\":1}})#.sort([(\"count\",-1)]).limit(50)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
